package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.IotRegion;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.IotRegionDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2020-10-05 16:33:40
 */
public class IotRegionConverts {

    private static Logger LOG = LoggerFactory.getLogger(IotRegionConverts.class);

    public static List<IotRegionDTO> convert(List<IotRegion> iotRegionList) {
        if (CollectionUtils.isEmpty(iotRegionList)) {
            return Collections.EMPTY_LIST;
        }

        List<IotRegionDTO> iotRegionListResult = Lists.newArrayList();
        for (IotRegion iotRegion : iotRegionList) {
            iotRegionListResult.add(convert(iotRegion));
        }
        return iotRegionListResult;
    }

    public static IotRegionDTO convert(IotRegion iotRegion) {
        try {
            return BeanUtils.copy(iotRegion, IotRegionDTO.class);
        } catch (Exception e) {
            LOG.error("IotRegion到IotRegionDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotRegion到IotRegionDTO转换失败", e);
        }
    }

    public static IotRegion convert(IotRegionDTO iotRegionDTO) {
        try {
            return BeanUtils.copy(iotRegionDTO, IotRegion.class);
        } catch (Exception e) {
            LOG.error("IotRegionDTO到IotRegion转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "IotRegionDTO到IotRegion转换失败", e);
        }
    }
}